home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / ibm / fasrc1p2.arc / AS.C next >
C/C++ Source or Header  |  1990-07-15  |  6KB  |  271 lines

  1. char mapdn();
  2. char *alloc();
  3. /*
  4.  *    as ---    cross assembler main program
  5.  */
  6. main(argc,argv)
  7. int    argc;
  8. char    **argv;
  9. {
  10.     char    **np;
  11.     char    *i;
  12.     FILE    *fopen();
  13.     int    j = 0;
  14.  
  15.     if(argc < 2){
  16.         printf("Usage:  %s [files] [- options]\n",argv[j]);
  17.         printf("Example:  %s FILE1.ASM FILE2.ASM - CRE L\n",argv[j]);
  18.         exit(1);
  19.         }
  20.       Argv = argv;
  21.       initialize();
  22.       while ((*argv[j] != '-') && (j<argc))
  23.        j++;
  24.       N_files = j-1;
  25.      if (j < argc )
  26.       {
  27.       argv[j]++;
  28.       while (j<argc)
  29.        {
  30.        for (i = argv[j]; *i != 0; i++)
  31.          if ((*i <= 'Z') && (*i >= 'A'))
  32.            *i = *i + 32;
  33.        if (strcmp(argv[j],"l")==0)
  34.          Lflag = 1;
  35.        else if (strcmp(argv[j],"nol")==0)
  36.          Lflag = 0;
  37.        else if (strcmp(argv[j],"c")==0)
  38.           Cflag = 1;
  39.        else if (strcmp(argv[j],"noc")==0)
  40.          Cflag = 0;
  41.        else if (strcmp(argv[j],"s")==0)
  42.          Sflag = 1;
  43.        else if (strcmp(argv[j],"cre")==0)
  44.          CREflag = 1;
  45.        else if ( *argv[j] ) {
  46.          printf("\nUnrecognized option on command line\n");
  47.          printf("Valid options are:\n");
  48.          fatal("   l  nol  c  noc  s  cre\n\n");
  49.          }
  50.         j++;
  51.        }
  52.       }
  53.     root = NULL;
  54.  
  55.     Cfn = 0;
  56.     np = argv;
  57.     Line_num = 0; /* reset cumulative line number */
  58.     while( ++Cfn <= N_files )
  59.      {
  60.         if((Fd = fopen(*++np,"r")) == NULL)
  61.             printf("as: can't open %s\n",*np);
  62.         else{
  63.             Cf_line_num = 0;  /* reset current file line number */
  64.             End = NO;       /* no END directive yet */
  65.             make_pass();
  66.             fclose(Fd);
  67.         }
  68.      }
  69.     if( Err_count == 0 ){
  70.         Pass++;
  71.         re_init();
  72.         Cfn = 0;
  73.         np = argv;
  74.         Line_num = 0;
  75.         while( ++Cfn <= N_files)
  76.          {
  77.             if((Fd = fopen(*++np,"r")) != NULL)
  78.                 {
  79.                 printf( "\n  Assembling %s\n", *np );
  80.                 Cf_line_num = 0;
  81.                 End = NO;
  82.                 make_pass();
  83.                 fclose(Fd);
  84.                  }
  85.          }
  86.         f_S9();                 /* output closing record */
  87.         if( Cflag )             /* if still counting cycles, then */
  88.            print_cycles();      /* print cycles counted */
  89.         if (Sflag == 1)
  90.           {
  91.             printf ("\f");
  92.             stable (root);
  93.           }
  94.         if (CREflag == 1)
  95.           {
  96.             printf ("\f");
  97.             cross (root);
  98.           }
  99.         }
  100.     printf("\n\nNumber of errors %d\n",Err_count);
  101.     printf("Number of warnings %d\n",Warn_count);
  102.     fwd_done();
  103.     exit(Err_count);
  104. }
  105.  
  106. initialize()
  107. {
  108.     FILE    *fopen();
  109.     char    c;
  110.     int    i = 0;
  111.  
  112. #ifdef DEBUG
  113.     printf("Initializing\n");
  114. #endif
  115.     Err_count = 0;
  116.     Warn_count = 0;
  117.     Pc        = 0;
  118.     E_pc      = 0;
  119.     Pass      = 1;
  120.     Ctotal      = 0;
  121.     N_page    = 0;
  122.     Cpflag    = 0;
  123.     Entry_set = NO;
  124.     Entry_pt  = 0;
  125.  
  126.     do {                     /* copy first file name into Obj_name */
  127.         c = Obj_name[i] = Argv[1][i];
  128.         i++;
  129.     } while( c && ( c != '.' ) && ( i < FILENAME_MAX ) );
  130.     Obj_name[--i] = EOS;
  131.     if( i >= ( FILENAME_MAX - 4 ) )
  132.         fatal("First file name too long");
  133.     strcat(Obj_name,".s19");  /* append .s19 to file name. */
  134.     if( (Objfil = fopen(Obj_name,"w")) == NULL)
  135.         fatal("Can't create object file");
  136.     fwdinit();    /* forward ref init */
  137.     localinit();    /* target machine specific init. */
  138. }
  139.  
  140. re_init()
  141. {
  142. #ifdef DEBUG
  143.     printf("Reinitializing\n");
  144. #endif
  145.     Err_count = 0;
  146.     Warn_count = 0;
  147.     Pc      = 0;
  148.     E_pc    = 0;
  149.     E_total = 0;
  150.     P_total = 0;
  151.     Ctotal    = 0;
  152.     N_page  = 0;
  153.     Cpflag  = 0;
  154.     Entry_set = NO;
  155.     Entry_pt  = 0;
  156.     fwdreinit();
  157. }
  158.  
  159. make_pass()
  160. {
  161.     char    *p;
  162.     char    *limit = Line+MAXBUF-1;
  163.     char    *fgets();
  164.  
  165. #ifdef DEBUG
  166.     printf("Pass %d\n",Pass);
  167. #endif
  168.     while( fgets(Line,MAXBUF,Fd) != (char *)NULL ){
  169.         for( p=Line; p<limit; p++ ) {   /* strip newline */
  170.             if( *p == NEWLINE ) {
  171.                 *p = EOS;
  172.                 break;
  173.                 }
  174.             }
  175.         Line_num++;
  176.         Cf_line_num++;
  177.         P_force = 0;    /* No force unless bytes emitted */
  178.         N_page = 0;
  179.            if(parse_line())
  180.             process();
  181.         if(Pass == 2 && Lflag && !N_page)
  182.             print_line();
  183.         if( Cpflag == 3 )        /* print cumulative cycles */
  184.             print_cycles();
  185.         P_total = 0;    /* reset byte count, */
  186.         Cpflag = 0;     /* cycle print flag, */
  187.         Cycles = 0;     /* and per instruction cycle count */
  188.         if( End ) break;
  189.         }
  190.     f_record();
  191. }
  192.  
  193.  
  194. /*
  195.  *    parse_line --- split input line into label, op and operand
  196.  */
  197. parse_line()
  198. {
  199.     register char *ptrfrm = Line;
  200.     register char *ptrto = Label;
  201.     char *limit = Label+MAXLAB-1;
  202.     char    *skip_white();
  203.  
  204.     if((*ptrfrm == '*') || (*ptrfrm == EOS) || (*ptrfrm == ';'))
  205.         return(0);    /* a comment line */
  206.  
  207.     while( (delim(*ptrfrm)== NO) && (ptrto < limit) )
  208.         *ptrto++ = *ptrfrm++;
  209.     if( *ptrfrm == ':' ) ptrfrm++;  /* allow one over limit for : */
  210.     if( delim(*ptrfrm) == NO ) {
  211.         while( delim(*ptrfrm) == NO )ptrfrm++;
  212.         warn("Label too long");
  213.         }
  214.     if(*--ptrto != ':')ptrto++;     /* ignore trailing : */
  215.     *ptrto = EOS;
  216.  
  217.     ptrfrm = skip_white(ptrfrm);
  218.  
  219.     ptrto = Op;
  220.     limit = Op+MAXOP-1;
  221.     while( (delim(*ptrfrm) == NO) && (ptrto < limit) )
  222.         *ptrto++ = mapdn(*ptrfrm++);
  223.     if( delim(*ptrfrm) == NO ) {
  224.         while( delim(*ptrfrm) == NO ) ptrfrm++;
  225.         error("Mnemonic/Directive too long");
  226.         }
  227.     *ptrto = EOS;
  228.  
  229.     ptrfrm = skip_white(ptrfrm);
  230.  
  231.     ptrto = Operand;
  232.     while( (*ptrfrm != EOS) )
  233.         *ptrto++ = *ptrfrm++;
  234.     *ptrto = EOS;
  235.  
  236. #ifdef DEBUG
  237.     printf("Label-%s-\n",Label);
  238.     printf("Op----%s-\n",Op);
  239.     printf("Operand-%s-\n",Operand);
  240. #endif
  241.     return(1);
  242. }
  243.  
  244. /*
  245.  *    process --- determine mnemonic class and act on it
  246.  */
  247. process()
  248. {
  249.     register struct oper *i;
  250.     struct oper *mne_look();
  251.  
  252.     Old_pc = Pc;        /* setup `old' program counter */
  253.     Optr = Operand;     /* point to beginning of operand field */
  254.  
  255.     if(*Op==EOS){        /* no mnemonic */
  256.         if( *Label ) install(Label,Pc);
  257.         }
  258.     else if( (i = mne_look(Op))== NULL) {
  259.         if( *Label ) install(Label,Pc);
  260.         error("Unrecognized Mnemonic");
  261.         }
  262.     else if( i->class == PSEUDO )
  263.         do_pseudo(i->opcode);
  264.     else{
  265.         if( *Label ) install(Label,Pc);
  266.         if(Cflag)Cycles = i->cycles;
  267.         do_op(i->opcode,i->class);
  268.         if(Cflag)Ctotal += Cycles;
  269.         }
  270. }
  271.